using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace homework3c1
{
    //链表节点
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }
        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }
    //泛型链表类
    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;
        public GenericList()
        {
            tail = head = null;
        }
        public Node<T> Head
        {
            get => head;
        }
        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }
        public void ForEach(Action<T> action)
        {
            Node<T> current = head;
            while(current!=null)
            {
                action(current.Data);
                current = current.Next;
            }
        }
        public T Min(Func<T, T, T> comparer)
        {
            if(head==null)
            {
                throw new InvalidOperationException("The list is empty.");
            }
            T min = head.Data;
            Node<T> current = head.Next;
            while (current != null)
            {
                min = comparer(min, current.Data);
                current = current.Next;
            }
            return min;
        }
        public T Max(Func<T, T, T> comparer)
        {
            if (head == null)
            {
                throw new InvalidOperationException("The list is empty.");
            }
            T max = head.Data;
            Node<T> current = head.Next;
            while (current != null)
            {
                max = comparer(max, current.Data);
                current = current.Next;
            }
            return max;
        }
        public T Sum(Func<T,T,T> accumulator)
        {
            if (head == null)
            {
                throw new InvalidOperationException("The list is empty.");
            }

            T sum = head.Data;
            Node<T> current = head.Next;
            while (current != null)
            {
                sum = accumulator(sum, current.Data);
                current = current.Next;
            }
            return sum;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //整型List
            GenericList<int> intlist = new GenericList<int>();
            for(int x = 0; x < 10; x++)
            {
                intlist.Add(x);
            }
            for (Node<int>node = intlist.Head; node != null; node = node.Next)
            {
                Console.WriteLine(node.Data);
            }
            intlist.ForEach(x => Console.WriteLine(x));

            int max = intlist.Max((a, b) => a > b ? a : b);
            Console.WriteLine("Max value: " + max);

            int min = intlist.Min((a, b) => a < b ? a : b);
            Console.WriteLine("Min value: " + min);

            int sum = intlist.Sum((a, b) => a + b);
            Console.WriteLine("Sum: " + sum);

            //字符串型List
            GenericList<string> strList = new GenericList<string>();
            for (int x = 0; x < 10; x++)
            {
                strList.Add("str" + x);
            }
            for(Node<string> node=strList.Head; node != null; node = node.Next
                )
            {
                Console.WriteLine(node.Data);
            }
        }
      
    }
}
